<!--
 * @title: 用户编辑页
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-04-10 23:37:22
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-06-06 11:46:42
 * @version: 1.0.3
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
-->
<template>
  <ht-drawer title="用户编辑页" :visible.sync="isShow">
    <!-- 用户编辑表单 -->
    <ht-form ref="userEditFm" v-model="formData" ref-form="userEditFm" divider="基础信息" :form-list="userEditList" />
    <template slot="footer">
      <ht-button type="primary" confirm="true" @click="handleSave">保存</ht-button>
    </template>
  </ht-drawer>
</template>
<script>
import { sleep } from '@/utils';
import { saveUser } from '@/api/ht.admin';
import { updateUser } from '@/api/ht.admin';
export default {
  name: 'UserDialog',
  props: {
    visible: {
      type: Boolean,
      required: false,
      default: false
    },
    rowData: {
      type: Object,
      required: false,
      default: undefined
    }
  },
  data() {
    return {
      isShow: this.visible,
      dialogVisible: this.visible,
      userEditList: [
        {
          type: 'text',
          format: [1, 'is_any', 20],
          label: 'id',
          field: 'id',
          hidden: true
        },
        {
          type: 'text',
          format: [1, 'is_any', 30],
          label: '用户账号',
          field: 'account',
          disabled: true
        },
        {
          type: 'text',
          format: [1, 'is_any', 30],
          label: '用户姓名',
          field: 'userName'
        },
        {
          type: 'dic',
          format: [0, 'isDic', 30],
          dicType: 'xb',
          label: '性别',
          field: 'sex'
        },
        {
          type: 'date',
          format: [0, 'isDate', 30],
          label: '出生日期',
          field: 'birthDate'
        },
        {
          type: 'dic',
          format: [0, 'isDic', 30],
          label: '状态',
          dicType: 'yxzt',
          field: 'status'
        }
      ],
      formData: {
        userName: 'xukaixing'
      }
    };
  },
  watch: {
    isShow(newValue, oldValue) {
      sleep(200).then(() => {
        this.$emit('update:visible', newValue);
      });
    }
  },
  created() {
    this.$crud.setEditValue(this.userEditList, this.rowData);
  },
  methods: {
    async handleSave() {
      // this.isShow = false;
      try {
        // 提交保存
        if (this.rowData.id) {
          const updateData = await this.$crud.update(this.$refs['userEditFm'], this.rowData.id, updateUser);
          if (this.$crud.updateResult(this.$parent.searchUserData, updateData, this.rowData.rownums))
            this.isShow = false;
        } else {
          const saveData = await this.$crud.save(this.$refs['userEditFm'], saveUser);
          if (this.$crud.insertResult(this.$parent.searchUserData, saveData))
            this.isShow = false;
        }
      } catch (e) {
        this.$notify.message(e, 'error');
      }
    }
  }
};
</script>
